package com.sup.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sup.model.Customer;
import com.sup.repository.CustomerRepository;

@Service
public class CustomerService {
	@Autowired
	CustomerRepository customerRepository;
	
	
	@Transactional
	public void saveCustomer(Customer customer){
		customerRepository.save(customer);
	}
	
	@Transactional
	public void updateCustomer(Customer customer){
		customerRepository.save(customer);
	}
	
	@Transactional
	public void removeCustomer(Long id){
		customerRepository.delete(id);
	}
	
	public Customer findCustomerById(Long id){
		return customerRepository.findOne(id);
	}
	
	public Page<Customer> findCustomers(int pageIndex,int size){
		Page<Customer> page=customerRepository.findCustomers(new PageRequest(pageIndex, size, Direction.ASC, "id"));
		return page;
	}
	public List<Customer> findAll(){
		return customerRepository.findAll();
	}

}
